package cao.oschina.bean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * 评论
 * <p/>
 * Created by Haidy on 15/4/1.
 */
public class Comment {

    public final static int CATALOG_NEWS = 1;
    public final static int CATALOG_POST = 2;
    public final static int CATALOG_TWEET = 3;
    public final static int CATALOG_ACTIVE = 4;
    public final static int CATALOG_MESSAGE = 4;//动态与留言都属于消息中心

    public final static int CLIENT_MOBILE = 2;
    public final static int CLIENT_ANDROID = 3;
    public final static int CLIENT_IPHONE = 4;
    public final static int CLIENT_WINDOWS_PHONE = 5;

    public String id;
    public String portrait;
    public String author;
    public String authorid;
    public String content;
    public String pubDate;
    public int appclient;
    public List<Refer> refers;

    public static List<Comment> parser(String xml) {
        List<Comment> commentList = new ArrayList<>();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
            Element root = document.getDocumentElement();
            NodeList newsList = root.getElementsByTagName("comment");
            for (int i = 0; i < newsList.getLength(); i++) {
                Comment comment = new Comment();
                NodeList childNodes = newsList.item(i).getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                        Node childNode = childNodes.item(j);
                        switch (childNode.getNodeName()) {
                            case "id":
                                comment.id = childNode.getFirstChild().getNodeValue();
                                break;
                            case "portrait":
                                if (childNode.hasChildNodes()) {
                                    comment.portrait = childNode.getFirstChild().getNodeValue();
                                }
                                break;
                            case "appclient":
                                comment.appclient = Integer.parseInt(childNode.getFirstChild().getNodeValue());
                                break;
                            case "author":
                                comment.author = childNode.getFirstChild().getNodeValue();
                                break;
                            case "authorid":
                                comment.authorid = childNode.getFirstChild().getNodeValue();
                                break;
                            case "pubDate":
                                comment.pubDate = childNode.getFirstChild().getNodeValue();
                                break;
                            case "content":
                                comment.content = childNode.getFirstChild().getNodeValue();
                                break;
                            case "refers":
                                if (childNode.hasChildNodes()) {
                                    comment.refers = Refer.parser(childNode.getChildNodes());
                                }
                                break;
                        }
                    }
                }
                commentList.add(comment);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析错误");
        }
        return commentList;
    }
}
